package com.example.android.ui.register;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;

import com.example.android.R;
import com.example.android.bean.Register;
import com.example.android.ui.base.BaseActivity;
import com.example.android.ui.login.LoginActivity;

public class RegisterActivity extends BaseActivity implements View.OnClickListener, RegisterContract.IRegisterView {
    private EditText account, mobile, password;
    private TextView login;
    private ImageView back;
    private Button register;
    private RegisterPresenter registerPresenter;

    @Override
    protected void initViews() {
        registerPresenter = new RegisterPresenter(this);
        account = find(R.id.register_edtext_account);
        mobile = find(R.id.register_edtext_mobile);
        password = find(R.id.register_edtext_password);
        back = find(R.id.register_back);
        login = find(R.id.register_text_login);
        register = find(R.id.register_btn_register);
        //点击事件
        back.setOnClickListener(this);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_back:
            case R.id.register_text_login:
                finish();
                break;
            case R.id.register_btn_register:
                String userAccount = account.getText().toString();
                String userMobile = mobile.getText().toString();
                String userPassword = password.getText().toString();
//                Log.i("RegisterActivity", userAccount + " " + userMobile + " " + userPassword);
                registerPresenter.addUser(userAccount, userMobile, userPassword);
            default:
                break;
        }
    }

    @Override
    public void addUserSuccess(Register register) {
//        Log.i("RegisterActivity",register.toString());
        if (register.getCode() == 0) {
            Toast.makeText(RegisterActivity.this, "注册成功 请登录", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(RegisterActivity.this, "账号已被注册 换一个吧", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void addUserError(Throwable throwable) {
        Log.i("RegisterActivity",throwable.toString());
    }
}
